package com.zhao.controller;

import com.sun.deploy.security.WSeedGenerator;
import com.zhao.pojo.Admin;
import com.zhao.pojo.SchoolCounsellor;
import com.zhao.pojo.Student;
import com.zhao.pojo.Superviser;
import com.zhao.service.DrivingLicenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class DrivingLicenseController {

    @Autowired
    DrivingLicenseService drivingLicenseService;

    ///////////////////////////  admin   /////////////////////////////////////////
    @ResponseBody
    @RequestMapping("admin-getOneDL.do")
    public String adminGetOneDL(String DLID,HttpSession session){

        Admin admin = (Admin)session.getAttribute("admin");
        if(admin == null){
            return "redirect:/adminLogin";
        }
        String res = drivingLicenseService.adminGetOneDL(admin,DLID,session);
        System.out.println(res);

        return res;
    }

    @RequestMapping("admin-getAllDrivingLicense.do")
    public String adminGetAllDrivingLicense(HttpSession session){

        Admin admin = (Admin)session.getAttribute("admin");
        if(admin == null){
            return "redirect:/adminLogin";
        }
        String res = drivingLicenseService.adminGetAllDL(admin,session);
        System.out.println(res);
        if(!res.equals("success")){
            return "404";
        }

        return "redirect:/admin-allDL";
    }


/////////////////////////////  校警     //////////////////////////////////////

    @ResponseBody
    @RequestMapping("superviserFindDrivingLicense.do")
    public String superviserFindDrivingLicense(String ebickID,HttpSession session){

        Superviser superviser = (Superviser)session.getAttribute("superviser");
        if(superviser==null)return "请先登录";

        System.out.println("find.do");
        String res = drivingLicenseService.superviserFindDrivingLicense(ebickID,session);
        System.out.println(res);

        return res;
    }

//***************************** 辅导员 ***********************************************




    ////驳回行驶证申请
    @RequestMapping("rejectDrivingLicenseApplication.do")
    public String rejectDrivingLicenseApplication(String drivingLicenseID,String rejectReason,HttpSession session){
        SchoolCounsellor schoolCounsellor = (SchoolCounsellor)session.getAttribute("schoolCounsellor");
        if(schoolCounsellor == null){
            System.out.println("请先登录");
            return "redirect:/";
        }
        System.out.println(rejectReason);
        String res =drivingLicenseService.rejectDrivingLicenseApplication(drivingLicenseID,rejectReason,session);
        System.out.println(res);
        if(!res.equals("success")){
            return "404";
        }

        return "redirect:/getCancelDrivingLicenseApplication.do";
    }

    //转到填写驳回申请原因
    @RequestMapping("toRejectDrivingLicense.do")
    public String toRejectDrivingLicense(String drivingLicenseID,HttpSession session){
        SchoolCounsellor schoolCounsellor = (SchoolCounsellor)session.getAttribute("schoolCounsellor");
        if(schoolCounsellor == null){
            System.out.println("请先登录");
            return "redirect:/";
        }
        session.setAttribute("drivingLicenseID",drivingLicenseID);
        System.out.println(drivingLicenseID);
        return "redirect:/newRejectApplicationReason";
    }

    ////驳回注销申请
    @RequestMapping("rejectCancelDrivingLicense.do")
    public String rejectCancelDrivingLicense(String drivingLicenseID,String rejectReason,HttpSession session){
        SchoolCounsellor schoolCounsellor = (SchoolCounsellor)session.getAttribute("schoolCounsellor");
        if(schoolCounsellor == null){
            System.out.println("请先登录");
            return "redirect:/";
        }
        System.out.println(rejectReason);
        String res =drivingLicenseService.rejectCancelDrivingLicense(drivingLicenseID,session);
        System.out.println(res);
        if(!res.equals("success")){
            return "404";
        }


        return "redirect:/getCancelDrivingLicenseApplication.do";
    }

    //转到填写驳回注销原因
    @RequestMapping("toRejectCancelDrivingLicense.do")
    public String toRejectCancelDrivingLicense(String drivingLicenseID,HttpSession session){
        SchoolCounsellor schoolCounsellor = (SchoolCounsellor)session.getAttribute("schoolCounsellor");
        if(schoolCounsellor == null){
            System.out.println("请先登录");
            return "redirect:/";
        }
        session.setAttribute("drivingLicenseID",drivingLicenseID);
        System.out.println(drivingLicenseID);
        return "newRejectCancelReason";
    }

    //辅导员
    //同意注销申请
    @RequestMapping("approveCancelDrivingLicense.do")
    public String approveCancelDrivingLicense(String drivingLicenseID,HttpSession session){
        SchoolCounsellor schoolCounsellor = (SchoolCounsellor)session.getAttribute("schoolCounsellor");
        if(schoolCounsellor == null){
            System.out.println("请先登录");
            return "redirect:/";
        }
        String res = drivingLicenseService.approveCancelDrivingLicense(drivingLicenseID,session);
        System.out.println(res);
        if(!res.equals("success")){
            return "404";
        }
        return "redirect:/getCancelDrivingLicenseApplication.do";
    }

    //辅导员
    //审批行驶证撤销申请
    @RequestMapping("getCancelDrivingLicenseApplication.do")
    public String getCancelDrivingLicenseApplication(HttpSession session){
        SchoolCounsellor schoolCounsellor = (SchoolCounsellor)session.getAttribute("schoolCounsellor");
        if(schoolCounsellor == null){
            System.out.println("请先登录");
            return "redirect:/";
        }
        String res = drivingLicenseService.getCancelDrivingLicenseApplication(session);
        System.out.println(res);
        if(!res.equals("success")){
            return "404";
        }
        return "redirect:/newShowCancelDLApplication";
    }

    //辅导员
    ////批准行驶证申请
    @RequestMapping("approveDrivingLicense.do")
    public String approveDrivingLicense(String drivingLicenseID,HttpSession session){
        SchoolCounsellor schoolCounsellor = (SchoolCounsellor)session.getAttribute("schoolCounsellor");
        if(schoolCounsellor == null){
            System.out.println("请先登录");
            return "redirect:/";
        }
        String res = drivingLicenseService.approveDrivingLicense(drivingLicenseID,session);
        System.out.println(res);
        if(!res.equals("success")){
            return "404";
        }
        return "redirect:/getApplicationOfDrivingLicense.do";
    }

    //辅导员
    //驳回申请
    @RequestMapping("rejectDrivingLicense.do")
    public String rejectDrivingLicense(String drivingLicenseID,HttpSession session){


        return null;
    }

    //辅导员
    //查看 所辖 学生的  行驶证申请
    @RequestMapping("getApplicationOfDrivingLicense.do")
    public String getApplicationOfDrivingLicense(HttpSession session){

        System.out.println("getApplicationOfDrivingLicense");

        SchoolCounsellor schoolCounsellor = (SchoolCounsellor)session.getAttribute("schoolCounsellor");
        if(schoolCounsellor == null){
            System.out.println("请先登录");
            return "redirect:/";
        }

        String res = drivingLicenseService.getApplicationOfDrivingLicense(schoolCounsellor,session);
        System.out.println(res);


        return "redirect:/newShowApplicationOfDL";
    }


/////////////////////////     学   生     /////////////////////////////////////////////

    //学生
    //撤销申请行驶证
    @RequestMapping("cancleApplyForDrivingLicense.do")
    public String cancleApplyForDrivingLicense(String ebickID, HttpSession session){
        Student student = (Student)session.getAttribute("student");
        if(student==null){
            return "redirect:/";
        }
        String res = drivingLicenseService.cancleApplyForDrivingLicense(ebickID,session);
        System.out.println(res);
        if(res.equals("没有可撤销的申请")){
            return "redirect:/newShowMyEbick";
        }
        else if(!res.equals("success")){
            return "404";
        }
        return "redirect:/getAllMyEbicks.do";
    }

    //学生
    //验证是否可以注销
    @ResponseBody
    @RequestMapping("toCancel.do")
    public String toCancel(String drivingLicenseID,HttpSession session){
        System.out.println("drivinglicenseid");
        System.out.println(drivingLicenseID);
        String res = drivingLicenseService.toCancel(drivingLicenseID,session);
        System.out.println(res);
        if(res.equals("当前不能注销")){
            return "no";
        }
        else if(res.equals("可以注销")){
            return "yes";
        }
        else{
            return "404";
        }
    }

    //学生
    //申请行驶证明
    @RequestMapping("applyForDrivingLicense.do")
    public String applyForDrivingLicense(String ebickID,HttpSession session){
        Student student = (Student)session.getAttribute("student");
        if(student==null){
            return "redirect:/";
        }
        //电动车 的 注册 和 申请行驶证明 分开
        ////申请信息由 辅导员老师  审核
        ///！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！1
        String res = drivingLicenseService.applyForDrivingLicense(ebickID,session);
        System.out.println(res);
        if(res.equals("当前不可以申请行驶证")){
            return "redirect:/newShowMyEbick";
        }
        else if(!res.equals("success")){
            return "404";
        }
        System.out.println("申请成功，返回展示,等待批准");
        return "redirect:/getAllMyEbicks.do";
    }

    //学生
    //注销行驶证   需要 导员批准  填写注销原因

    @ResponseBody
    @RequestMapping("cancelDrivingLicense.do")
    public String cancelDrivingLicense(@RequestParam("drivingLicenseID") String drivingLicenseID, @RequestParam("cancelReason") String cancelReason, HttpSession session){

        System.out.println("cancelDrivingLicense.do");
        System.out.println(drivingLicenseID);
        System.out.println(cancelReason);
        String res = drivingLicenseService.cancelDrivingLicense(drivingLicenseID,cancelReason,session);
        System.out.println(res);
        if(!res.equals("success")){
            System.out.println(res);
            return "注销申请失败";
        }
        return "success";
    }

    //学生
    //查看自己的行驶证明
    @RequestMapping("getDrivingLicenseImpl")
    public String getDrivingLicenseImpl(HttpSession session){
        Student student = (Student)session.getAttribute("student");
        if(student==null){
            return "redirect:/";
        }
        System.out.println("查看自己的行驶证明");
        String res = drivingLicenseService.getDrivingLicenseImpl(session);
        System.out.println(res);
       if(res.equals("error")){
            return "redirect:/404";
        }else if(res.equals("没有行驶证")){
           session.setAttribute("getDrivingLicenseMessage","没有行驶证");
           return "redirect:/newShowDrivingLicense";
           //return "noDrivingLicense";
       }

        return "redirect:/newShowDrivingLicense";
    }

}
